import tushare as ts
from sqlalchemy import create_engine
import logging
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt



def painter_book_pick_event_print_xy():
    """
    散点图
    鼠标点击并输出坐标
    可以根据坐标获取绑定元素对象进行下一步操作
    :return:
    """
    fig, ax = plt.subplots()
    ax.plot(np.random.random(100), 'o', picker=5)  # 5 points tolerance
    # ax.plot(x, k1, 'o', picker=1)
    text = ax.text(0.5, 0.5, 'event', ha='center', va='center', fontdict={'size': 20})
    def on_pick(event):
        line = event.artist
        x_data, y_data = line.get_data()
        ind = event.ind

        print('on pick line:', ind, np.array([x_data[ind], y_data[ind]]).T)
        # print('tickit:', df.iloc[ind][['ts_code', 'name', 'pb', 'roe']])
        info = "Name={};button={};\n(x,y):{},{}(Dx,Dy):{:3.2f},{:3.2f}".format(
            event.name, event.button, event.x, event.y, event.xdata, event.ydata)

        text.set_text(info)
        fig.canvas.draw_idle()

    fig.canvas.mpl_connect('pick_event', on_pick)
    plt.show()





painter_book_pick_event_print_xy()